/* =函数定义
-----------------------------------------------------------------------------*/
/* 把json的键名更新到选择框选项。
 * 参数一“选择框对象”。
 * 参数二“json对象”。
 * */
function updateSelectOption(_select, _json) {
	if(!_select || !_json) {
		return;
	}
	var optionHTML = "<option value=\"0\"></option>";
	var value = 0;
	for(var i in _json) {
		optionHTML += '<option value="' + ++value + '">' + i + '</option>';
	}
	_select.innerHTML = optionHTML;
}
/* =添加事件
-----------------------------------------------------------------------------*/
addLoadEvent(calcHTMLFontSize);
window.onresize = calcHTMLFontSize;
/* =选择框刷新数据
-----------------------------------------------------------------------------*/
/*院系、专业、班级选择框对象*/
var g_sexSelect = document.querySelector("select[name='sex']");
var g_departmentSelect = document.querySelector("select[name='department']");
var g_majorSelect = document.querySelector("select[name='major']");
var g_classSelect = document.querySelector("select[name='class']");
/*院系、专业、班级文本输入框对象*/
var g_sexTextInput = document.querySelector("input[name='sex']");
var g_departmentTextInput = document.querySelector("input[name='department']");
var g_majorTextInput = document.querySelector("input[name='major']");
var g_classTextInput = document.querySelector("input[name='class']");
/*清除性别、院系、专业、班级文本输入框name属性，否则会提交重复参数*/
g_sexTextInput.name = "";
g_departmentTextInput.name = "";
g_majorTextInput.name = "";
g_classTextInput.name = "";
/*专业、班级json对象*/
var g_selectedMajorJson = null;
var g_selectedClassJson = null;
/*更新院系选择框*/
updateSelectOption(g_departmentSelect, g_njit);
/*添加性别选择框选项改变事件*/
g_sexSelect.onchange = function() {
	g_sexTextInput.value = g_sexSelect.options[g_sexSelect.selectedIndex].text;
}
/*添加院系选择框选项改变事件*/
g_departmentSelect.onchange = function() {
	g_departmentTextInput.value = g_departmentSelect.options[g_departmentSelect.selectedIndex].text;
	/*如果院系文本输入框有内容*/
	if(g_departmentTextInput.value) {
		g_selectedMajorJson = g_njit[g_departmentTextInput.value];
		updateSelectOption(g_majorSelect, g_selectedMajorJson);
//		g_majorSelect.disabled = false;
	} else {
//		g_majorSelect.disabled = true;
	}
	g_majorSelect.selectedIndex = 0;
	g_majorSelect.onchange();
}
/*添加专业选择框改变事件*/
g_majorSelect.onchange = function() {
	g_majorTextInput.value = g_majorSelect.options[g_majorSelect.selectedIndex].text;
	/*如果专业文本输入框有内容*/
	if(g_majorTextInput.value) {
		g_selectedClassJson = g_selectedMajorJson[g_majorTextInput.value];
		updateSelectOption(g_classSelect, g_selectedClassJson);
//		g_classSelect.disabled = false;
	} else {
//		g_classSelect.disabled = true;
	}
	g_classSelect.selectedIndex = 0;
	g_classSelect.onchange();
}
/*添加班级选择框改变事件*/
g_classSelect.onchange = function() {
	g_classTextInput.value = g_classSelect.options[g_classSelect.selectedIndex].text;
}
/* =表单发送
-----------------------------------------------------------------------------*/
function sendForm() {
	var request = new XMLHttpRequest();
	request.open("POST", "bm.php");

	var data =
		"name=" + document.querySelector("input[name='name']").value +
		"&sex=" + g_sexSelect.value +
		"&no=" + document.querySelector("input[name='no']").value +
		"&department=" + g_departmentSelect.value +
		"&major=" + g_majorSelect.value +
		"&class=" + g_classSelect.value +
		"&qq=" + document.querySelector("input[name='qq']").value +
		"&tel=" + document.querySelector("input[name='tel']").value +
		"&skill=" + document.querySelector("input[name='skill']").value +
		"&expect=" + document.querySelector("input[name='expect']").value;

	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send(data);
	request.onreadystatechange = function() {
		if(request.readyState === 4) {
			if(request.status === 200) {
				console.log(request.responseText);
				var retJSON = JSON.parse(request.responseText);
				alert(retJSON.msg);
			} else {
				alert("服务器错误代码：" + request.status);
			}
		}
	}
	return false;
}